<template>
  <div class="refundConfig">
    <el-col v-bind="grid">
      <el-form :model="refundForm" v-loading="refundLoading" :rules="refundRules" label-width="185px" ref="refundRefs" class="refundCls">
        <el-form-item prop="consigneeName">
          <template v-slot:label>退货收货人姓名  <el-tooltip class="item" effect="dark" content="退货收货人姓名" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-model="refundForm.consigneeName" type="text" placeholder="退货收货人姓名"></el-input>
        </el-form-item>
        <el-form-item prop="consigneeMobile">
          <template v-slot:label>退货收货人电话  <el-tooltip class="item" effect="dark" content="退货收货人电话" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-model="refundForm.consigneeMobile" type="text" placeholder="退货收货人电话"></el-input>
        </el-form-item>
        <el-form-item prop="consigneeAddress">
          <template v-slot:label>退货收货人地址  <el-tooltip class="item" effect="dark" content="退货收货人地址" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
          <el-input v-model="refundForm.consigneeAddress" type="text" placeholder="退货收货人地址"></el-input>
        </el-form-item>
      </el-form>
    </el-col>
    <el-col :span="24">
      <el-col :span="3">
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="submitAddRefund('refundRefs')" :loading="loadingBtn">提交</el-button>
        </el-row>
      </el-col>
    </el-col>
  </div>
</template>

<script>
  import {
    getRefundListApi,
    putRefundContentApi
  } from '@/api/setting';

  export default {
    name: "index",
    data() {
      const consigneeMobileValidator = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请填写收货人电话'));
        } else if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value)) {
          callback(new Error('请填写正确的电话'));
        } else {
          callback();
        }
      }
      return {
        grid: {
          xl: 8,
          lg: 12,
          md: 12,
          sm: 12,
          xs: 24
        },
        refundLoading: false,
        loadingBtn: false,
        refundForm: {
          consigneeName: '',
          consigneeMobile: '',
          consigneeAddress: ''
        },
        refundRules: {
          consigneeName: [
            { required: true, message: '请填写收货人姓名', trigger: 'blur' }
          ],
          consigneeMobile: [
            { required: true, validator: consigneeMobileValidator, trigger: 'blur' }
          ],
          consigneeAddress: [
            { required: true, message: '请填写收货人地址', trigger: 'blur' }
          ]
        }
      };
    },
    mounted() {
      this.getRefundList();
    },
    methods: {
      // 获取退款设置
      getRefundList() {
        this.$set(this, 'refundLoading', true)
        getRefundListApi().then(res => {
          this.$set(this, 'refundForm', res);
          this.$set(this, 'refundLoading', false);
        }).catch(() => {
          this.$set(this, 'refundLoading', false);
        });
      },
      submitAddRefund(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$set(this, 'loadingBtn', true);
            putRefundContentApi(this.refundForm).then(res => {
              this.$message.success('修改成功');
              this.getRefundList();
              this.$set(this, 'loadingBtn', false);
            }).catch(() => {
              this.$set(this, 'loadingBtn', false);
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>
